import Vue from 'vue'
import VueRouter from  'vue-router'
import Config from './Config'
import store from "../stores"

// 1.安装
Vue.use(VueRouter)


// 解决导航取消问题
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {

return originalPush.call(this, location).catch(err => err)

}

// 2.创建路由对象
var router = new VueRouter(Config)

router.beforeEach(function(to,from,next){
    if(to.meta.auth){
        // 需要登录才能访问
        if(store.state.loginUser.isLoading){
            next({name:"Auth",query:{returnurl:to.fullPath}})
        }else if(store.state.loginUser.data){
            next();//允许进入
        }else{
            next({name:"Login"})
        }
    }else{
        // 都可以访问
        next();
    }
  next()
});

export default router;